package model

import (
	"github.com/google/uuid"
	"github.com/shopspring/decimal"
	"gorm.io/gorm"
)

// Product 商品结构体
type Product struct {
	BaseModel
	Name       string          `json:"name"`
	SKU        string          `json:"sku"`
	Desc       string          `json:"desc"`
	StockCount int             `json:"StockCount"`
	Price      decimal.Decimal `json:"price"`
	// 分类id
	CategoryId int64 `json:"categoryId"`
	// 分类
	Category Category `json:"-"`
}

// TableName 声明表名
func (Product) TableName() string {
	return "shopping_product"
}

// NewProduct 实例化
func NewProduct(name string, desc string, stockCount int, price decimal.Decimal, cid int64) *Product {
	return &Product{
		Name:       name,
		Desc:       desc,
		StockCount: stockCount,
		Price:      price,
		CategoryId: cid,
		BaseModel:  BaseModel{DelFlag: 0},
	}
}

// BeforeSave 保存商品之前生成商品sku
func (p *Product) BeforeSave(tx *gorm.DB) (err error) {
	if p.SKU == "" {
		p.SKU = uuid.New().String()
	}
	return nil
}
